package org.judo.test.controllers.admin;

import org.judo.components.uicontrol.ChoiceControl;
import org.judo.controller.PageController;
import org.judo.database.Database;
import org.judo.propertyobject.Prop;
import org.judo.propertyobject.PropertyObject;
import java.util.*;
import org.judo.controller.DefaultAction;
import org.judo.test.models.SecurityRule;

public class SecurityRuleController extends PageController 
{
	private Database sysDB ;
	private int id ;
	private String securityruleid ;
	private int siteresourceid ;
	private int roleid ;
	private ArrayList results = new ArrayList();
	@Prop private ChoiceControl chRoles = new ChoiceControl();
	@Prop private ChoiceControl chPages = new ChoiceControl();

	public void editSecurityRule() throws Exception
	{
		chPages.define(sysDB.query("select url as display,siteResourceId as code from Site_Resource order by url"));
		chRoles.define(sysDB.query("select roleName as display,roleId as code from Role order by roleName"));
		
		PropertyObject obj = sysDB.find(parse("select * from Security_rule where SECURITYRULEID = ${id}"));
		
		chPages.populate(obj.get("siteresourceid"));
		chRoles.populate(obj.get("roleid"));
		
		copyProps(obj);
	}

	public void setId(int id) 
	{
		this.id = id;
	}

	public int getId() 
	{
		return this.id;
	}

	public void setSecurityruleid(String securityruleid) 
	{
		this.securityruleid = securityruleid;
	}

	public String getSecurityruleid() 
	{
		return this.securityruleid;
	}

	public void updateSecurityRule() throws Exception
	{
		showPage("/admin/Security_rule_controller/display_security_rule.do");
		
		SecurityRule obj = (SecurityRule)sysDB.find(SecurityRule.class,id);
		
		obj.copyProps(this);
		sysDB.update("SECURITY_RULE",obj);
	}

	public void setSiteresourceid(int siteresourceid) 
	{
		this.siteresourceid = siteresourceid;
	}

	public int getSiteresourceid() 
	{
		return this.siteresourceid;
	}

	public void setRoleid(int roleid) 
	{
		this.roleid = roleid;
	}

	public int getRoleid() 
	{
		return this.roleid;
	}

	@DefaultAction
	public void displaySecurityRule() throws Exception
	{
		results = (ArrayList)sysDB.query("select securityRuleId,url,roleName from SECURITY_RULE,Site_Resource,Role where SECURITY_RULE.siteResourceId = Site_Resource.siteResourceId and SECURITY_RULE.roleId = Role.roleId");
	}

	public void setResults(ArrayList results) 
	{
		this.results = results;
	}

	public ArrayList getResults() 
	{
		return this.results;
	}

	public void createSecurityRule() throws Exception
	{
		chPages.define(sysDB.query("select url as display,siteResourceId as code from Site_Resource order by url"));
		chRoles.define(sysDB.query("select roleName as display,roleId as code from Role order by roleName"));
	}

	public void insertSecurityRule() throws Exception
	{
		showPage("/admin/Security_rule_controller/display_security_rule.do");
		SecurityRule obj = new SecurityRule();
		obj.copyProps(this);
		sysDB.insert("SECURITY_RULE",obj);
	}

	public void detailSecurityRule() throws Exception
	{
		PropertyObject obj = sysDB.find(parse("select securityRuleId,url,roleName from SECURITY_RULE,Site_Resource,Role where SECURITY_RULE.siteResourceId = Site_Resource.siteResourceId and SECURITY_RULE.roleId = Role.roleId and SECURITYRULEID = ${id}"));
		copyProps(obj);
	}

	public void deleteSecurityRule() throws Exception
	{
		showPage("/admin/Security_rule_controller/display_security_rule.do");
		sysDB.delete(this,"delete from SECURITY_RULE where SECURITYRULEID = ${id}");
	}

}
